package com.ccys.cms.service.userManagement.impl;


import com.ccys.cms.service.userManagement.TblLabResultWebService;
import com.ccys.common.third.push.firebase.FireBaseUtil;
import com.ccys.common.third.push.huawei.HuaWeiPush;
import com.ccys.common.third.push.ios.IosNotificationUtil;
import com.ccys.common.util.Kit;
import com.ccys.common.util.StringUtil;
import com.ccys.common.util.easyExcel.EasyExcelUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.userManagement.TblLabResultDTO;
import com.ccys.db.entity.TblLabResult;
import com.ccys.db.entity.TblMember;
import com.ccys.db.entity.TblSupplier;
import com.ccys.db.mapper.TblLabResultMapper;
import com.ccys.db.mapper.TblMemberMapper;
import com.ccys.db.mapper.TblSupplierMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.vo.userManagement.SelectSupplierWebVO;
import com.ccys.db.vo.userManagement.TblLabResultWebVO;

import com.ccys.db.vo.userManagement.TblLabResultWebVOExportVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
 * Laboratory results 和Third party health screening管理
 */
@Service
@Slf4j
public class TblLabResultWebServiceImpl extends BaseServiceImpl<TblLabResult> implements TblLabResultWebService {

    @Autowired(required = false)
    private TblLabResultMapper tblLabResultMapper;
    @Autowired
    private TblSupplierMapper supplierMapper;
    @Autowired(required = false)
    private TblMemberMapper memberMapper;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblLabResultDTO dto) {
        //通用接口不需校验
        /*if(StringUtil.isEmpty(dto.getMemId())) {
            return ResponseResult.error("MemId is not null");
        }*/
        if(StringUtil.isNotEmpty(dto.getTypes())) {
            dto.setTypes(Kit.getWhereByIds(dto.getTypes()));
        }
        PageUtils.startPage(request);
        List<TblLabResultWebVO> list = this.tblLabResultMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(TblLabResult entity) {
        String memId = "";
        if (StringUtils.isBlank(entity.getId())) {
            if(StringUtil.isEmpty(entity.getMemId())) {
                return ResponseResult.error("memId is not null");
            }
            // 新增
            //后端默认手动上传
            entity.setType(1);
            this.saveEntity(entity);
            memId = entity.getMemId();
        } else {
            // 编辑
            TblLabResult entityById = this.findEntityById(entity.getId());
            if(entityById == null) {
                return ResponseResult.error("update object is null");
            }
            memId = entityById.getMemId();
            if(entityById.getType() !=null && 0 == entityById.getType()) {
                if("verified".equals(entityById.getStatus())) {
                    return ResponseResult.error("verified status not modifiable");
                }
                if(StringUtil.isNotEmpty(entity.getStatus()) && "Verified".equals(entity.getStatus())) {
                    //2024-01-03 确认需求此处修改status 值 是存英文，不算修改状态不做状态记录
                    //修改确认状态 管理人员手动确认(verified)之后，推送消息通知 app。在 verified 之前可以进行修改
                    TblMember tblMember = this.memberMapper.selectByPrimaryKey(memId);
                    if(tblMember != null) {
                        if(tblMember.getDeviceType() != null && StringUtil.isNotEmpty(tblMember.getDeviceId())) {
                            // TODO cloudFly 发送内容待确认
                            String title = "test";
                            String body = "test";
                            String hideMsg = "";
                            List<String> tokenList = new ArrayList<>();
                            tokenList.add(tblMember.getDeviceId());
                            try {
                                if(1 == tblMember.getDeviceType()) {
                                    IosNotificationUtil.push(tokenList,title,body,true,null);
                                }else if(2 == tblMember.getDeviceType()) {
                                    FireBaseUtil.pushSingle(tblMember.getDeviceId(),title,body);
                                }else if(3 == tblMember.getDeviceType()) {
                                    HuaWeiPush.pushToByToken(title,body,hideMsg,tokenList);
                                }
                            }catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }
                }
            }

            this.updateEntityById(entity);

        }
        //当前页面二次弹窗确认是否上传并且推送至 app。修改的话不会做推送和消息
        TblMember tblMember = this.memberMapper.selectByPrimaryKey(memId);
        if(tblMember != null) {
            if(tblMember.getDeviceType() != null && StringUtil.isNotEmpty(tblMember.getDeviceId())) {
                // TODO cloudFly 发送内容待确认
                String title = "test";
                String body = "test";
                String hideMsg = "";
                List<String> tokenList = new ArrayList<>();
                tokenList.add(tblMember.getDeviceId());
                try {
                    if(1 == tblMember.getDeviceType()) {
                        IosNotificationUtil.push(tokenList,title,body,true,null);
                    }else if(2 == tblMember.getDeviceType()) {
                        FireBaseUtil.pushSingle(tblMember.getDeviceId(),title,body);
                    }else if(3 == tblMember.getDeviceType()) {
                        HuaWeiPush.pushToByToken(title,body,hideMsg,tokenList);
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(TblLabResult dto) {
        TblLabResult entity = new TblLabResult();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        this.updateEntityById(entity);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listSelectSupplier(TblSupplier dto) {
        if(StringUtil.isEmpty(dto.getType())) {
            dto.setType("service");
        }
        //PageDto.startPage();
        List<SelectSupplierWebVO> list = this.supplierMapper.listSelectSupplier(dto);
        //PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult exportLabResult(HttpServletRequest request, HttpServletResponse response, TblLabResultDTO dto) {
        List<TblLabResultWebVOExportVO> list = this.tblLabResultMapper.exportListObjectsCms(dto);
        EasyExcelUtil.export(response, list, TblLabResultWebVOExportVO.class, "exportLabResult.csv");
        return ResponseResult.success();
    }


}